import django_filters
from .models import StockAlert, StockMovement, Supplier, PurchaseOrder


class StockAlertFilter(django_filters.FilterSet):
    alert_type = django_filters.ChoiceFilter(choices=StockAlert.ALERT_TYPES)
    is_resolved = django_filters.BooleanFilter()
    book = django_filters.NumberFilter(field_name='book__id')
    start_date = django_filters.DateFilter(field_name='created_at', lookup_expr='gte')
    end_date = django_filters.DateFilter(field_name='created_at', lookup_expr='lte')
    
    class Meta:
        model = StockAlert
        fields = ['alert_type', 'is_resolved', 'book', 'start_date', 'end_date']


class StockMovementFilter(django_filters.FilterSet):
    movement_type = django_filters.ChoiceFilter(choices=StockMovement.MOVEMENT_TYPES)
    book = django_filters.NumberFilter(field_name='book__id')
    start_date = django_filters.DateFilter(field_name='created_at', lookup_expr='gte')
    end_date = django_filters.DateFilter(field_name='created_at', lookup_expr='lte')
    
    class Meta:
        model = StockMovement
        fields = ['movement_type', 'book', 'start_date', 'end_date']


class SupplierFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(field_name='name', lookup_expr='icontains')
    city = django_filters.CharFilter(field_name='city', lookup_expr='icontains')
    province = django_filters.CharFilter(field_name='province', lookup_expr='icontains')
    is_active = django_filters.BooleanFilter()
    
    class Meta:
        model = Supplier
        fields = ['name', 'city', 'province', 'is_active']


class PurchaseOrderFilter(django_filters.FilterSet):
    status = django_filters.ChoiceFilter(choices=PurchaseOrder.STATUS_CHOICES)
    supplier = django_filters.NumberFilter(field_name='supplier__id')
    start_date = django_filters.DateFilter(field_name='order_date', lookup_expr='gte')
    end_date = django_filters.DateFilter(field_name='order_date', lookup_expr='lte')
    expected_date = django_filters.DateFilter(field_name='expected_date', lookup_expr='gte')
    
    class Meta:
        model = PurchaseOrder
        fields = ['status', 'supplier', 'start_date', 'end_date', 'expected_date']
